<template>
    <div class="unite">
        <el-row>
            <el-col :span="24" class="unite-1">
                <el-button type="primary" @click="openVisible" :disabled="showData">编辑</el-button>
                <el-button type="danger" @click="deleteTableData" :disabled="showData">删除</el-button>
            </el-col>
            <el-col :span="24">
                <el-table
                    :data="tabData"
                    tooltip-effect="dark"
                    style="width: 100%"
                    border
                    size="mini"
                    height="220px"
                    @selection-change="handleSelectionChange"
                >
                    <el-table-column type="selection" width="55"> </el-table-column>
                    <el-table-column label="条目编号" width="120" prop="entryCode" fixed="left" show-overflow-tooltip> </el-table-column>
                    <el-table-column
                        :key="col.prop"
                        :prop="col.prop"
                        :label="col.title"
                        :width="col.width"
                        show-overflow-tooltip
                        v-for="col in Columns"
                    >
                        <template slot-scope="scope">
                            <span v-if="col.funcBody"> {{ col.funcBody(scope.row[col.prop], scope.row) }} </span>
                            <!-- <span v-else v-html="getTbalesData(scope.row, col.prop)"></span> -->
                            <span
                                v-else
                                :style="
                                    col.prop === 'entryStatusValid' && scope.row.entryStatusValid
                                        ? 'color:green'
                                        : col.prop === 'entryStatusValid' && !scope.row.entryStatusValid
                                        ? 'color:red'
                                        : ''
                                "
                            >
                                {{ getTbalesData(scope.row, col.prop) }}</span
                            >
                        </template>
                    </el-table-column>
                </el-table>
            </el-col>
        </el-row>
        <div class="unite-dialog">
            <el-dialog v-if="visible" drag title="联合设置：" :visible.sync="visible" width="800px">
                <RiskTrantableSetting
                    ref="trans"
                    url="/entry/list/CodeOrName"
                    index-key="entryCode"
                    left-placeholder="编号/名称"
                    right-placeholder="编号/名称"
                    :columns="columnsTran"
                    :entryType="entryType"
                    ltLevel="null"
                    :premise="false"
                    :right-table-data="trantRightData"
                    :search-names="['entryCode', 'entryName']"
                    :filter-self="entryObj.entryCode"
                ></RiskTrantableSetting>
                <div slot="footer" class="dialog-footer">
                    <el-button size="mini" @click="visible = false"> 取 消 </el-button>
                    <el-button size="mini" type="primary" @click="submitSettingData">确 定 </el-button>
                </div>
            </el-dialog>
        </div>
    </div>
</template>
<script lang="ts">
import { Component, Prop, Vue, Watch } from 'vue-property-decorator';
import RiskTrantableSetting from '@/common/components/risk-trantable-setting.vue';
import { mixins } from 'vue-class-component';
import RiskEmitter from '@/common/mixis/risk-emitter.vue';
import DictionConstUtil, { SGU_CACHE } from '@/utils/diction-const-util';
import { numberFormat, unitShow } from '@/utils/tools';

@Component({
    name: 'UniteSetting',
    components: {
        RiskTrantableSetting,
    },
})
export default class UniteSetting extends mixins(RiskEmitter) {
    @Prop() joinRiskEntryControls!: any[];
    @Prop() showData: boolean;
    @Prop() entryControls;
    tabData: any[] = [];
    selectedList: any[] = [];
    entryType: any = '0';
    entryTypes: any = null;
    Columns: any[] = [
        {
            title: '风险说明',
            prop: 'entryName',
            width: 250,
        },
        {
            title: '变动序号',
            prop: 'changeNo',
            width: 80,
        },
        {
            title: '风险类型',
            prop: 'entryType',
            width: 130,
        },
        {
            title: '生效日期',
            prop: 'beginDate',
            width: 100,
        },
        {
            title: '失效日期',
            prop: 'endDate',
            width: 100,
        },
        {
            title: '优先级',
            prop: 'level',
            width: 65,
        },
        {
            title: '状态',
            prop: 'entryStatusValid',
            width: 50,
        },
        {
            title: '风险等级',
            prop: 'controlGrade',
            width: 85,
        },
        {
            title: '作用范围',
            prop: 'riskCalTypes',
            width: 100,
        },
        {
            title: '阈值类型1',
            prop: 'thresholdType1',
            width: 100,
        },
        {
            title: '阈值1',
            prop: 'thresholdValue1',
            width: 100,
            funcBody: (value, record) => (value != undefined ? numberFormat(value.replace(/[^\d.]/g, ''), record.unit) + unitShow(record.unit) : ''),
        },
        {
            title: '阈值类型2',
            prop: 'thresholdType2',
            width: 100,
        },
        {
            title: '阈值2',
            prop: 'thresholdValue2',
            width: 100,
            funcBody: (value, record) => (value != undefined ? numberFormat(value.replace(/[^\d.]/g, ''), record.unit) + unitShow(record.unit) : ''),
        },
        {
            title: '阈值类型3',
            prop: 'thresholdType3',
            width: 100,
        },
        {
            title: '阈值3',
            prop: 'thresholdValue3',
            width: 100,
            funcBody: (value, record) => (value != undefined ? numberFormat(value.replace(/[^\d.]/g, ''), record.unit) + unitShow(record.unit) : ''),
        },
        {
            title: '比较方向',
            prop: 'compareType',
            width: 100,
        },
        {
            title: '账户层次',
            prop: 'originalAccountId',
            width: 150,
        },
        {
            title: '控制类型',
            prop: 'accountControlType',
            width: 150,
        },
        {
            title: '控制账户',
            prop: 'accountControlValue',
            width: 150,
        },
        {
            title: '排除账户',
            prop: 'accountDebarValue',
            width: 120,
        },
        {
            title: '排除账户组',
            prop: 'accountGroupDebarValue',
            width: 150,
        },
        {
            title: '修改日期',
            prop: 'modifyDate',
            width: 120,
        },
        {
            title: '修改时间',
            prop: 'modifyTime',
            width: 100,
        },
        {
            title: '修改人',
            prop: 'modifyUser',
            width: 150,
        },
        {
            title: '条目来源',
            prop: 'sourceType',
            width: 100,
        },
        {
            title: '自定义分类',
            prop: 'customType',
            width: 110,
        },
        {
            title: '条目角色分类',
            prop: 'entryRoleCode',
            width: 180,
        },
    ];
    columnsTran = [
        {
            label: '条目编号',
            prop: 'entryCode',
            width: 100,
        },
        {
            label: '风险说明',
            prop: 'entryName',
        },
    ];
    trantRightData: any[] = [];
    visible: boolean = false;
    fundGroups: any[] = [];
    funds: any[] = [];
    cutomTypes: any = null;
    EntryRLdata: any = {};
    Empowerment: any = {};
    stateLists: any[] = [];
    alertControlGrade: any[] = [];
    EntryTypeList: any[] = [];
    customTypesList: any[] = [];
    EntryRoleList: any[] = [];
    entryObj = {}; // 自身条目id

    async mounted() {
        console.time('UniteSetting');
        this.tabData = this.joinRiskEntryControls;
        this.trantRightData = this.joinRiskEntryControls;
        // 风险等级
        this.alertControlGrade = DictionConstUtil.PojoToList(DictionConstUtil.ControlGrade);
        // 风险类型
        this.EntryTypeList = DictionConstUtil.PojoToList(DictionConstUtil.EntryType);
        // 自定义风险
        this.customTypesList = (await DictionConstUtil.FNCustomType()) || [];
        // 条目角色
        this.EntryRoleList = DictionConstUtil.PojoToList(DictionConstUtil.EntryRole);
        // 条目组
        this.funds = (await DictionConstUtil.FNFundInfo()) || [];
        // 产品组信息
        this.fundGroups = (await DictionConstUtil.FNFundGroup()) || [];
        // 自定义风险分类
        this.cutomTypes = DictionConstUtil.ListToPojo(this.customTypesList || []) || {};

        this.funds = (await DictionConstUtil.FNFundInfo()) || [];
        // 产品组信息
        this.fundGroups = (await DictionConstUtil.FNFundGroup()) || [];
        // 自定义风险分类
        this.cutomTypes = DictionConstUtil.ListToPojo(this.customTypesList || []) || {};
        // 条目数据
        let fubds = await SGU_CACHE('FundInfo', '/fundInfo/list', []);
        let local = [];
        for (let itme of fubds) {
            let datas = itme.fundCode + '-' + itme.fundName;
            local.push({ label: datas, id: itme.fundCode });
        }
        // 风险类别数据
        let entryType = await SGU_CACHE('EntryType', '/entry/entryType', []);
        let typeLocal = [];
        for (let itme of entryType) {
            let datas = itme.entryTypeId + ' ' + itme.entryTypeName;
            typeLocal.push({ label: datas, id: itme.entryTypeId });
        }
        console.timeEnd('UniteSetting');
    }

    @Watch('joinRiskEntryControls', { deep: true })
    watchjoinRiskEntryControls() {
        this.tabData = this.joinRiskEntryControls;
        this.trantRightData = this.joinRiskEntryControls;
        this.setUniteData();
    }

    @Watch('entryControls', { immediate: true, deep: true })
    watchEntryControls(newdata) {
        if (newdata) {
            if (newdata.entryCode) {
                this.entryObj = newdata;
            }
        }
    }

    // 选择全部
    dropdownClick(val: any): void {
        this.selectedList = [];
        for (let i = 0; i < this.tabData.length; i++) {
            this.selectedList.push(this.tabData[i].entryCode);
        }
    }

    // 处理table中的数据
    getTbalesData(data: any, props: any) {
        let fundPojo = {};
        let fundGroupPojo = {};
        // 产品信息
        let funds = this.funds;
        // 产品组信息
        let fundGroups = this.fundGroups;
        // 自定义风险分类
        let cutomTypes = this.cutomTypes;
        funds.forEach(item => {
            fundPojo[item.fundCode] = item.fundNameSt;
        });
        fundGroups.forEach(item => {
            fundGroupPojo[item.fundGroupCode] = item.fundGroupName;
        });
        // -1 也在展示范围内
        fundPojo['-1'] = '全部选择';
        fundGroupPojo['-1'] = '全部选择';
        let datas = data[props];
        if (props == 'entryType') {
            // eslint-disable-next-line no-unused-expressions
            return DictionConstUtil.EntryType[datas];
        } else if (props == 'entryStatusValid') {
            if (data[props] == true) {
                let span = '启用';
                return span;
            } else {
                let span = '禁用';
                return span;
            }
        } else if (props == 'controlGrade') {
            return DictionConstUtil.ControlGrade[datas];
        } else if (props == 'riskCalTypes') {
            let result = '';
            for (let i = 0; i < datas.length; i++) {
                result += DictionConstUtil.RiskCalType[datas[i]] + ',';
            }
            return result;
        } else if (props == 'thresholdType1' || props == 'thresholdType2' || props == 'thresholdType3') {
            return DictionConstUtil.ThresholdType[datas];
        } else if (props == 'compareType') {
            return DictionConstUtil.CompareDirection[datas];
        } else if (props == 'originalAccountId') {
            return DictionConstUtil.OriginalAccountType[datas];
        } else if (props == 'accountControlType') {
            return DictionConstUtil.AccountControlType[datas];
        } else if (props == 'accountDebarValue') {
            if (datas && datas.length > 0) {
                let fundNames = datas.map(item => fundPojo[item]);
                return fundNames.join(',');
            } else {
                return datas;
            }
        } else if (props == 'accountControlValue') {
            if (datas && datas.length > 0) {
                if (String(data.originalAccountId) === '0') {
                    let fundNames = datas.map(item => fundPojo[item]);
                    return fundNames.join(',');
                } else {
                    let fundGroupNames = datas.map(item => fundGroupPojo[item]);
                    return fundGroupNames.join(',');
                }
            } else {
                return datas;
            }
        } else if (props == 'accountGroupDebarValue') {
            if (datas && datas.length > 0) {
                if (String(data.originalAccountId) === '0') {
                    let fundNames = datas.map(item => fundPojo[item]);
                    return fundNames.join(',');
                } else {
                    let fundGroupNames = datas.map(item => fundGroupPojo[item]);
                    return fundGroupNames.join(',');
                }
            } else {
                return datas;
            }
        } else if (props == 'sourceType') {
            return DictionConstUtil.SourceType[datas];
        } else if (props == 'customType') {
            return cutomTypes[datas];
        } else if (props == 'entryRoleCode') {
            if (datas != null) {
                let entryRoleCodes = datas.split(',');
                if (entryRoleCodes.length > 0 && entryRoleCodes.length === 1) {
                    return DictionConstUtil.EntryRole[datas];
                } else {
                    let name = '';
                    for (let i = 0; i < entryRoleCodes.length; i++) {
                        name += DictionConstUtil.EntryRole[entryRoleCodes[i]] + ',';
                    }
                    return name;
                }
            }
        } else {
            return datas;
        }
    }

    // 打开
    openVisible(): void {
        this.entryType = this.entryTypes;
        this.visible = true;
    }

    // 删除
    deleteTableData(): void {
        let oldTableData = this.tabData;
        let newDataSource = [];
        for (let i = 0; i < oldTableData.length; i++) {
            if (this.selectedList.includes(oldTableData[i].entryCode)) {
                continue;
            }
            newDataSource.push(oldTableData[i]);
        }
        this.tabData = [];
        this.tabData = newDataSource;
        this.trantRightData = this.tabData;
        this.setUniteData();
    }

    handleSelectionChange(val: any) {
        this.selectedList = [];
        for (let i = 0; i < val.length; i++) {
            this.selectedList.push(val[i].entryCode);
        }
    }

    submitSettingData() {
        this.visible = false;
        const rightData = (this.$refs.trans as any).getRightAllData();
        this.tabData = rightData;
        this.trantRightData = rightData;
        this.setUniteData();
    }

    // 回调函数
    setUniteData() {
        this.$emit('set-unite-data', this.tabData);
    }
}
</script>
<style lang="scss" scoped>
.unite {
    color: #222;

    .unite-1 {
        padding: 2px 10px;
    }

    .unite-dialog /deep/ .el-dialog .el-dialog__body {
        max-height: none;
        height: 66vh;
    }
}
</style>
